package com.study.webapp.connectionpool;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import javax.swing.JSpinner.DateEditor;

public class DefaultThreadPool<Job extends Runnable> implements ThreadPool<Job> {
	//最大线程数
	private static final int  MAX_WORKER_NUMBERS = 10;
	//默认线程数
	private static final int DEFAULT_WORKER_NUMBERS = 5;
	//最小线程数
	private static final int MIN_WORKER_NUMBERS = 1;
	//一个工作列表，将会向里面插入工作
	private final LinkedList<Job> jobs = new LinkedList<Job>();
	//工作者列表
	private final List<Worker> workers = Collections.synchronizedList(new ArrayList<Worker>());
	//工作者线程的数量
	private int workerNum = DEFAULT_WORKER_NUMBERS;
	//线程编码生成
	private AtomicLong threadNum = new AtomicLong();
	
	public DefaultThreadPool() {
		initializeWorkers(DEFAULT_WORKER_NUMBERS);
	}
	
	public DefaultThreadPool(int num){
		workerNum = num > MAX_WORKER_NUMBERS ? MAX_WORKER_NUMBERS : num < MIN_WORKER_NUMBERS ? MIN_WORKER_NUMBERS : num;
		initializeWorkers(workerNum);
	}
	
	@Override
	public void execute(Job job) {
		if(job != null){
			synchronized (jobs){
				jobs.addLast(job);
				jobs.notify();
			}
		}
	}

	@Override
	public void shutdown() {
		for(Worker worker : workers){
			worker.shutdown();
		}
	}

	@Override
	public void addWorker(int num) {
		synchronized (jobs) {
			if(num+this.workerNum > MAX_WORKER_NUMBERS){
				num = MAX_WORKER_NUMBERS - this.workerNum;
			}
			initializeWorkers(num);
			this.workerNum += num;
		}
	}

	@Override
	public void removeWorker(int num) {
		synchronized (jobs) {
			if(num >= this.workerNum){
				throw new IllegalArgumentException("");
			}
			int count = 0;
			while (count < num) {
				Worker worker = workers.get(count);
				if(workers.remove(worker)){
					worker.shutdown();
					count++;
				}
			}
			this.workerNum -= count;
		}
	}

	@Override
	public int getJobSize() {
		return jobs.size();
	}
	
	//初始化线程工作者
	private void initializeWorkers(int num){
		for(int i = 0;i<num;i++){
			Worker worker = new Worker();
			workers.add(worker);
			Thread thread = new Thread(worker,"Thread-worker-"+threadNum.incrementAndGet());
			thread.start();
		}
	}
	
	//工作者，负责消费任务
	class Worker implements Runnable {
		private volatile boolean running = true;
		@Override
		public void run() {
			while(running){
				Job job = null;
				synchronized (job) {
					//如果是空得就等待
					while (jobs.isEmpty()){
						try {
							jobs.wait();
						} catch (Exception e) {
							//
							Thread.currentThread().interrupt();
							return;
						}
					}
					//取出一个JOB
					job = jobs.removeFirst();
				}
				if(job != null){
					try {
						job.run();
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		
		}
		
		public void shutdown(){
			running = false;
		}
		
	}
	

}
